package org.crystalrange.softwares.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;

import org.crystalrange.BaseController;
import org.crystalrange.softwares.model.Software;
import org.crystalrange.softwares.service.SoftwareService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class SoftwareListController extends BaseController {

	protected Software selectedSoftware;
	protected SoftwareService softwareService;
	protected List<Software> softwares;
	protected Listbox listboxSoftware;
	protected Search searchSoftware;

	public Software getSelectedSoftware() {
		return selectedSoftware;
	}

	public void setSelectedSoftware(Software selectedSoftware) {
		this.selectedSoftware = selectedSoftware;
	}

	public SoftwareService getSoftwareService() {
		return softwareService;
	}

	public void setSoftwareService(SoftwareService softwareService) {
		this.softwareService = softwareService;
	}

	public List<Software> getSoftwares() {
		return softwares;
	}

	public void setSoftwares(List<Software> softwares) {
		this.softwares = softwares;
	}

	public void onClick$toolbarbuttonSoftwareCreate(Event event)
			throws Exception {
		this.softwareCreate();
	}

	public void onClick$menuitemSoftwareCreate(Event event) throws Exception {
		this.softwareCreate();
	}

	public void onClick$toolbarbuttonSoftwareEdit(Event event) throws Exception {
		this.softwareEdit();
	}

	public void onClick$menuitemSoftwareEdit(Event event) throws Exception {
		this.softwareEdit();
	}

	public void onClick$toolbarbuttonSoftwareDelete(Event event)
			throws Exception {
		this.softwareDelete();
	}

	public void onClick$menuitemSoftwareDelete(Event event) throws Exception {
		this.softwareDelete();
	}

	public void onClick$toolbarbuttonSoftwareSearch(Event event)
			throws Exception {
		this.softwareSearch();
	}

	public void onClick$menuitemSoftwareSearch(Event event) throws Exception {
		this.softwareSearch();
	}

	public void onClick$toolbarbuttonSoftwareRefresh(Event event)
			throws Exception {
		this.softwareRefresh();
	}

	public void onClick$menuitemSoftwareRefresh(Event event) throws Exception {
		this.softwareRefresh();
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.searchSoftware = new Search(Software.class);
		this.searchSoftware.setMaxResults(100);
		this.searchSoftware.addFetch("createdBy");
		this.softwares = this.softwareService.search(this.searchSoftware);
		component.setAttribute("controller", this);
	}

	private void softwareCreate() throws Exception {
		this.execution.sendRedirect("~./zul/html/software.zul");
	}

	private void softwareEdit() throws Exception {
		if (this.selectedSoftware != null) {
			this.execution.sendRedirect("~./zul/html/software.zul?Id="
					+ this.selectedSoftware.getId().toString());
		}
	}

	private void softwareDelete() throws Exception {
		Set<Listitem> selectedItems = this.listboxSoftware.getSelectedItems();

		if (selectedItems.size() > 0) {
			if (Messagebox.YES == Messagebox.show("Delete these records?",
					"Delete?", Messagebox.YES | Messagebox.NO,
					Messagebox.QUESTION)) {
				for (Listitem selectedItem : selectedItems) {
					Software software = (Software) selectedItem.getValue();
					this.softwareService.remove(software);
				}
				this.selectedSoftware = null;
			}
		} else {
			this.message.setText("Please select at least one item!");
			this.message.setIcon("/img/exclamation.png");
		}
		this.softwares = this.softwareService.search(this.searchSoftware);
		this.binder.loadAll();
	}

	private void softwareSearch() throws Exception {
		this.execution.setAttribute("searchSoftware", this.searchSoftware);

		Window window = (Window) this.execution.createComponents(
				"~./zul/html/softwareSearch.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			this.searchSoftware = (Search) this.execution
					.getAttribute("searchSoftware");
		}

		this.softwares = this.softwareService.search(this.searchSoftware);
		this.binder.loadAll();
	}

	private void softwareRefresh() throws Exception {
		this.softwares = this.softwareService.search(this.searchSoftware);
		this.binder.loadAll();
	}
}
